import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { User } from './user.entity';

@Injectable()
export class UserService {
  constructor(
    @InjectRepository(User)
    private userRepository: Repository<User>,
  ) {}

  // 查询所有用户
  findAll(): Promise<User[]> {
    return this.userRepository.find();
  }

  // 查询单个用户
  findOne(id: number): Promise<User> {
    return this.userRepository.findOne({ where: { id } });
  }

  // 创建用户
  create(user: User): Promise<User> {
    return this.userRepository.save(user);
  }

  // 更新用户
  async update(id: number, user: User): Promise<User> {
    await this.userRepository.update(id, user);
    return this.userRepository.findOne({ where: { id } });
  }

  // 删除用户
  remove(id: number): Promise<void> {
    return this.userRepository.delete(id).then(() => null);  
  }
}